package com.jh.paymentchannel.business.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import com.jh.paymentchannel.pojo.*;
import com.jh.paymentchannel.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jh.paymentchannel.business.TopupPayChannelBusiness;


@Service
public class TopupPayChannelBusinessImpl implements TopupPayChannelBusiness {

    @Autowired
    private TopupPayChannelRepository topupchannelrepository;

    @Autowired
    private ChannelDetailRepository channelDetailRepository;

    @Autowired
    private MiMaiRegisterRepository mimaRegisterRepository;

    @Autowired
    private SDJRepository sdjRepository;

    @Autowired
    private SDJAreaRepository sdjAreaRepository;

    @Autowired
    private SDJBindCardRepository sdjBindCardRepository;

    @Autowired
    private JFRepository jfRepository;

    @Autowired
    private JFMQuickBindCardRepository jfmQuickBindCardRepository;

    @Autowired
    private JFCoinRepository jfCoinRepository;

    @Autowired
    private JFApiRepository jfApiRepository;

    @Autowired
    private YHBankCodeRepository yhBankCodeRepository;

    @Autowired
    private CJRegisterRepository cjRegisterRepository;

    @Autowired
    private CJBindCardRepository cjBindCardRepository;

    @Autowired
    private CJQuickBindCardRepository cjQuickBindCardRepository;

    @Autowired
    private XJYCoinRepository xjyCoinRepository;

    @Autowired
    private XJYCoinBindCardRepository xjyCoinBindCardRepository;

    @Autowired
    private HLRegisterRepository hlRegisterRepository;

    @Autowired
    private WFRegisterRepository wfRegisterRepository;

    @Autowired
    private JFQuickRegisterRepository jfQuickRegisterRepository;

    @Autowired
    private JFshangaoBindCardRepository jfshangaoBindCardRepository;
    @Autowired

    private BankNumCodeRepository bankNumCodeRepository;
    @Autowired

    private BankInfoCodeRepository bankInfoCodeRepository;

    @Autowired
    private EntityManager em;

    @Autowired
    private MiLianRegisterRepository miLianRegisterRepository;

    @Autowired
    private MiLianQuickRegisterRepository miLianQuickRegisterRepository;

    @Autowired
    private MiLianQuickBindCardRepository miLianQuickBindCardRepository;

    @Autowired
    private HZQuickBankRepository hzQuickBankRepository;

    @Override
    public TopupPayChannelRoute getTopupChannelByBrandcode(String brandcode, String channelType, String channelTag) {
        TopupPayChannelRoute topupPayChannelRoute;
        topupPayChannelRoute = topupchannelrepository.getTopupChannelRoute(brandcode, channelType, channelTag);
        return topupPayChannelRoute;
    }

    @Transactional
    @Override
    public TopupPayChannelRoute saveTopupPayChannelRoute(TopupPayChannelRoute topupPayChannelRoute) {
        TopupPayChannelRoute result = topupchannelrepository.save(topupPayChannelRoute);
        em.flush();
        return result;
    }

    @Override
    public List<TopupPayChannelRoute> getTopupChannelByBrandId(String brandcode, String channelType, String channelTag) {
        List<TopupPayChannelRoute> topupPayChannelRouteList = new ArrayList<TopupPayChannelRoute>();
        if (brandcode != null && !brandcode.equals("")) {
            if (channelType != null && !channelType.equals("")) {
                if (channelTag != null && !channelTag.equals("")) {
                    TopupPayChannelRoute topupPayChannelRoute = new TopupPayChannelRoute();
                    topupPayChannelRoute = topupchannelrepository.getTopupChannelRoute(brandcode, channelType, channelTag);
                    if (topupPayChannelRoute != null) {
                        topupPayChannelRouteList.add(topupPayChannelRoute);
                    }
                } else {
                    topupPayChannelRouteList = topupchannelrepository.getPayChannelRoute(brandcode, channelType);
                }

            } else if (channelTag != null && !channelTag.equals("")) {
                topupPayChannelRouteList = topupchannelrepository.getPayChannelRouteChannelTag(brandcode, channelTag);

            } else {
                topupPayChannelRouteList = topupchannelrepository.getPayChannelRouteBybrandcode(brandcode);
            }

        } else if (channelType != null && !channelType.equals("")) {

            if (channelTag != null && !channelTag.equals("")) {

                topupPayChannelRouteList = topupchannelrepository.getPayChannelRouteBychannelcode(channelTag, channelType);
            } else {

                topupPayChannelRouteList = topupchannelrepository.getPayChannelRoute(channelType);
            }

        } else if (channelTag != null && !channelTag.equals("")) {

            topupPayChannelRouteList = topupchannelrepository.getPayChannelRouteBychannelTag(channelTag);
        } else {

            topupPayChannelRouteList = topupchannelrepository.getPayChannelRoute();
        }

        return topupPayChannelRouteList;

    }

    @Override
    public List<TopupPayChannelRoute> getPayChannelByBrandcode(String brandcode) {
        return topupchannelrepository.getPayChannelRouteBybrandcode(brandcode);
    }

    /***
     * 通过标识获取后台通道数据
     *
     * **/
    @Override
    public ChannelDetail getChannelDetailByTag(String channelTag) {
        return channelDetailRepository.getChannelDetailByTag(channelTag);
    }

    /***
     * 通过标识获取后台通道数据
     *
     * **/
    @Override
    public List<ChannelDetail> getChannelDetail() {
        return channelDetailRepository.getChannelDetail();
    }

    @Transactional
    @Override
    public MiMaiRegister createMiMaiRegister(MiMaiRegister mimaiRegister) {

        MiMaiRegister result = mimaRegisterRepository.save(mimaiRegister);
        em.flush();
        return result;
    }

    @Override
    public MiMaiRegister getMiMaiRegister(long userid, String bankcard) {

        return mimaRegisterRepository.getMiMaiRegisterByUseridAndBankCard(userid, bankcard);
    }

    @Override
    public MiMaiRegister getMiMaiRegister(long userid) {

        return mimaRegisterRepository.getMiMaiRegisterByUserid(userid);

    }

    //一键配置通道路由
    @Transactional
    @Override
    public TopupPayChannelRoute configTopupPayChannelRoute(TopupPayChannelRoute topupPayChannelRoute) {
        TopupPayChannelRoute result = (TopupPayChannelRoute) topupchannelrepository.save(topupPayChannelRoute);
        em.flush();
        em.clear();
        return result;
    }

    @Transactional
    @Override
    public SDJRegister createSDJRegister(SDJRegister sdjRegister) {
        return sdjRepository.save(sdjRegister);
    }

    @Override
    public SDJRegister getSDJRegister(long userid) {
        SDJRegister sdjRegisterByUserid = sdjRepository.getSDJRegisterByUserid(userid);
        return sdjRegisterByUserid;
    }

    @Override
    public SDJRegister getSDJRegister(long userid, String bankcard) {
        SDJRegister sdjRegisterByUseridAndBankCard = sdjRepository.getSDJRegisterByUseridAndBankCard(userid, bankcard);
        return sdjRegisterByUseridAndBankCard;
    }

    //通过省市的名字查询到省市的编码
    @Override
    public String getSDJAreaByName(String name) {
        String sdjAreaByName = sdjAreaRepository.getSDJAreaByName(name);
        return sdjAreaByName;
    }

    @Override
    public SDJBindCard getBankCard(String userid, String bankCard) {
        return sdjBindCardRepository.findByUserIdAndBankCard(userid, bankCard);
    }

    @Override
    public SDJBindCard createSDJBindCard(SDJBindCard sdjBindCard) {

        return sdjBindCardRepository.save(sdjBindCard);
    }

    @Transactional
    @Override
    public JFRegister createJFRegister(JFRegister jfRegister) {
        JFRegister result = jfRepository.save(jfRegister);
        em.flush();
        return result;
    }

    @Override
    public JFRegister getJFRegister(long userId) {

        return jfRepository.getJFRegisterByUserid(userId);
    }

    @Override
    public JFRegister getJFRegister(long userId, String bankCard) {

        return jfRepository.getJFRegisterByUseridAndBankCard(userId, bankCard);
    }

    @Transactional
    @Override
    public JFCoinRegister createJFCoinRegister(JFCoinRegister jfCoinRegister) {
        JFCoinRegister result = jfCoinRepository.save(jfCoinRegister);
        em.flush();
        return result;
    }

    @Override
    public JFCoinRegister getJFCoinRegister(long userId) {
        return jfCoinRepository.getJFCoinRegisterByUserid(userId);
    }

    @Override
    public JFCoinRegister getJFCoinRegister(long userId, String bankCard) {
        return jfCoinRepository.getJFCoinRegisterByUseridAndBankCard(userId, bankCard);
    }

    @Transactional
    @Override
    public JFApiRegister createJFApiRegister(JFApiRegister jfApiRegister) {
        JFApiRegister result = jfApiRepository.save(jfApiRegister);
        em.flush();
        return result;
    }

    @Override
    public JFApiRegister getJFApiRegister(String bankCard) {
        return jfApiRepository.getJFApiRegisterByBankCard(bankCard);
    }

    @Override
    public String getBankCodeByBankName(String bankName) {
        return yhBankCodeRepository.getBankCodeByBankName(bankName);
    }

    @Transactional
    @Override
    public CJRegister createCJRegister(CJRegister cjRegister) {
        CJRegister result = cjRegisterRepository.save(cjRegister);
        em.flush();
        return result;
    }

    @Override
    public CJRegister getCJRegister(long userId) {
        return cjRegisterRepository.getCJRegisterByUserid(userId);
    }

    @Override
    public CJRegister getCJRegister(long userId, String bankCard) {
        return cjRegisterRepository.getCJRegisterByUseridAndBankCard(userId, bankCard);
    }

    @Override
    public CJBindCard getCJBankCard(String userid, String bankCard) {
        return cjBindCardRepository.findByUserIdAndBankCard(userid, bankCard);
    }

    @Transactional
    @Override
    public CJBindCard createCJBindCard(CJBindCard cjBindCard) {
        CJBindCard result = cjBindCardRepository.save(cjBindCard);
        em.flush();
        return result;
    }

    @Override
    public CJRegister getCJRegisterByRateCode1(long userId, String rateCode1) {
        return cjRegisterRepository.getCJRegisterByUseridAndRateCode1(userId, rateCode1);
    }

    @Override
    public CJRegister getCJRegisterByRateCode2(long userId, String rateCode2) {

        return cjRegisterRepository.getCJRegisterByUseridAndRateCode2(userId, rateCode2);
    }

    @Override
    public CJQuickBindCard getCJQuickBindCard(String userid, String bankCard) {
        return cjQuickBindCardRepository.findCJQByUserIdAndBankCard(userid, bankCard);
    }

    @Transactional
    @Override
    public CJQuickBindCard createCJQuickBindCard(CJQuickBindCard CJQuickBindCard) {
        CJQuickBindCard result = cjQuickBindCardRepository.save(CJQuickBindCard);
        em.flush();
        return result;
    }

	
	/*//鑫佳宇通道
	@Transactional
	@Override
	public XJYApiRegister createXJYApiRegister(XJYApiRegister xjyApiRegister) {
		XJYApiRegister result = xjyApiRepository.save(xjyApiRegister);
		em.flush();
		return result;
	}

	@Override
	public XJYApiRegister getXJYApiRegister(String bankCard) {
		return xjyApiRepository.getXJYApiRegisterByBankCard(bankCard);
	}*/

    @Override
    public HLRegister getHLRegisterByBankCardAndCardType(String bankCard, String cardType) {
        return hlRegisterRepository.findByBankCardAndCardType(bankCard, cardType);
    }

    @Override
    public HLRegister getHLRegisterByPhoneAndCardType(String phone, String cardType) {
        return hlRegisterRepository.findByPhoneAndCardType(phone, cardType);
    }

    @Override
    public WFRegister getWFRegisterByBankCard(String bankCard) {
        return wfRegisterRepository.getWFRegisterByBankCard(bankCard);
    }

    @Transactional
    @Override
    public WFRegister createWFRegister(WFRegister wfRegister) {
        WFRegister result = wfRegisterRepository.save(wfRegister);
        em.flush();
        return result;
    }

    @Override
    public XJYCoinRegister getXJYCoinRegisterByPhoneAndBankCard(String phone, String bankCard) {
        return xjyCoinRepository.getXJYCoinRegisterByPhoneAndBankCard(bankCard, phone);
    }

    @Override
    public XJYCoinRegister getXJYCoinRegisterByBankCard(String bankCard) {
        return xjyCoinRepository.getXJYCoinRegisterByBankCard(bankCard);
    }

    @Transactional
    @Override
    public XJYCoinRegister createXJYCoinRegister(XJYCoinRegister xjyCoinRegister) {
        XJYCoinRegister result = xjyCoinRepository.save(xjyCoinRegister);
        em.flush();
        return result;
    }

    @Override
    public XJYCoinBindCard getXJYCoinBindCardByBankCard(String bankCard) {
        return xjyCoinBindCardRepository.getXJYCoinBindCardByBankCard(bankCard);
    }

    @Transactional
    @Override
    public XJYCoinBindCard createXJYCoinBindCard(XJYCoinBindCard xjyCoinBindCard) {
        XJYCoinBindCard result = xjyCoinBindCardRepository.save(xjyCoinBindCard);
        em.flush();
        return result;
    }

    @Override
    public JFQuickRegister getJFQuickRegisterByPhoneAndBankCard(String phone, String bankCard) {
        return jfQuickRegisterRepository.getJFQuickRegisterByPhoneAndBankCard(phone, bankCard);
    }

    @Override
    public JFQuickRegister getJFQuickRegisterByBankCard(String bankCard) {
        return jfQuickRegisterRepository.getJFQuickRegisterByBankCard(bankCard);
    }

    @Transactional
    @Override
    public JFQuickRegister createJFQuickRegister(JFQuickRegister JFQuickRegister) {
        JFQuickRegister result = jfQuickRegisterRepository.save(JFQuickRegister);
        em.flush();
        return result;
    }

    @Override
    public JFQuickRegister getJFQuickRegisterByRateCode1(String phone, String rateCode1) {
        return jfQuickRegisterRepository.getJFQuickRegisterByPhoneAndRateCode1(phone, rateCode1);
    }

    @Override
    public JFQuickRegister getJFQuickRegisterByRateCode2(String phone, String rateCode2) {
        return jfQuickRegisterRepository.getJFQuickRegisterByPhoneAndRateCode2(phone, rateCode2);
    }

    @Override
    public JFshangaoBindCard getJFshangaoBindCard(String phone, String bankCard) {
        return jfshangaoBindCardRepository.findByPhoneAndBankCard(phone, bankCard);
    }

    @Transactional
    @Override
    public JFshangaoBindCard createJFshangaoBindCard(JFshangaoBindCard JFshangaoBindCard) {
        JFshangaoBindCard result = jfshangaoBindCardRepository.save(JFshangaoBindCard);
        em.flush();
        return result;
    }

    @Override
    public BankInfoCode getBankInfoCodeByBankName(String bankName) {
        return bankInfoCodeRepository.getBankInfoCodeByBankName(bankName);
    }

    @Override
    public BankNumCode getBankNumCodeByBankName(String bankName) {
        return bankNumCodeRepository.getBankNumCodeByBankName(bankName);
    }

    @Override
    public JFQuickRegister getJFQuickRegisterByIdCard(String idCard) {
        return jfQuickRegisterRepository.getJFQuickRegisterByIdCard(idCard);
    }

    @Transactional
    @Override
    public JFMQuickBindCard createJFMQuickBindCard(JFMQuickBindCard jfmQuickBindCard) {
        JFMQuickBindCard result = jfmQuickBindCardRepository.save(jfmQuickBindCard);
        em.flush();
        return result;
    }

    @Override
    public JFMQuickBindCard getJFMQuickBindCard(long userid, String bankCard) {
        return jfmQuickBindCardRepository.getJFMQuickBindCard(userid, bankCard);
    }

    @Transactional
    @Override
    public MiLianRegister createMiLianRegister(MiLianRegister miLianRegister) {
        MiLianRegister result = miLianRegisterRepository.save(miLianRegister);
        em.flush();
        return result;
    }

    @Override
    public MiLianRegister getMiLianRegister(long userId) {
        return miLianRegisterRepository.getMiLianRegisterByUserid(userId);
    }

    @Override
    public MiLianRegister getMiLianRegister(long userId, String debitRate) {
        return miLianRegisterRepository.getMiLianRegisterByUseridAndRate(userId, debitRate);
    }

    @Override
    @Transactional
    public Integer deleteMiLianRegister(long userId, String bankCard) {
        return miLianRegisterRepository.deleteMiLianRegisterByUIdAndBankId(userId, bankCard);
    }

    @Override
    public MiLianQuickRegister getMiLianQuickRegister(long userId) {
        return miLianQuickRegisterRepository.getMiLianQuickRegisterByUserid(userId);
    }

    @Override
    public MiLianQuickRegister getMiLianQuickRegister(long userId, String bankCard) {
        return miLianQuickRegisterRepository.getMiLianQuickRegisterByUseridAndBankCard(userId, bankCard);
    }

    @Transactional
    @Override
    public MiLianQuickRegister createMiLianQuickRegister(MiLianQuickRegister miLianQuickRegister) {
        MiLianQuickRegister result = miLianQuickRegisterRepository.save(miLianQuickRegister);
        em.flush();
        return result;
    }

    @Override
    @Transactional
    public Integer deleteMiLianRegisterByUIdAndBankId(long userId, String bankCard) {
        return miLianQuickRegisterRepository.deleteMiLianRegisterByUIdAndBankId(userId, bankCard);
    }

    @Override
    @Transactional
    public Integer updateMiLianRegisterStateByUIdAndBankId(long userId, String bankCard) {
        return miLianQuickRegisterRepository.updateMiLianRegisterStateByUIdAndBankId(userId, bankCard);
    }

    @Transactional
    @Override
    public MiLianQuickBindCard createMiLianQuickBindCard(MiLianQuickBindCard miLianQuickBindCard) {
        MiLianQuickBindCard result = miLianQuickBindCardRepository.save(miLianQuickBindCard);
        em.flush();
        return result;
    }

    @Override
    public MiLianQuickBindCard getMiLianQuickBindCardByUseridAndBankCard(String userId, String bankCard) {
        return miLianQuickBindCardRepository.getMiLianQuickBindCardByUseridAndBankCard(userId, bankCard);
    }

    @Override
    public Integer updateMiLianBindCardStateByIdAndBankId(String userId, String bankCard) {
        return miLianQuickBindCardRepository.updateMiLianBindCardStateByIdAndBankId(userId, bankCard);
    }

    @Override
    public String getHZQuickBankCodeByBankName(String bankName) {
        return hzQuickBankRepository.getHZQuickBankCodeByBankName(bankName);
    }

}
